`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    23:35:06 11/24/2012 
// Design Name: 
// Module Name:    vga_module 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module vga_module(
	input clk_i, rst_i,
	output hsync_o, vsync_o,
	output reg [7:0] rgb_o	
    );
	 
	 wire video_on;
	 wire [9:0]pixel_x, pixel_y;
	 wire [7:0] square_rgb;
	 wire [2:0] square_white_on, square_black_on;
	 wire color =1;
	 //Variables para peones blancos.
	 wire [7:0] pawn_white_rgb,pawn_2white_rgb,pawn_3white_rgb,pawn_4white_rgb,pawn_5white_rgb,pawn_6white_rgb,pawn_7white_rgb, pawn_8white_rgb;
	 wire [2:0]  pawn_1on,  pawn_2on,  pawn_3on,  pawn_4on,  pawn_5on,  pawn_6on,  pawn_7on,  pawn_8on;
	 
	  //Variables para peones negros.
	  wire [7:0] pawn_black_rgb,pawn_2black_rgb,pawn_3black_rgb,pawn_4black_rgb,pawn_5black_rgb,pawn_6black_rgb,pawn_7black_rgb, pawn_8black_rgb;
	  wire [2:0]  pawnblack_1on,  pawnblack_2on,  pawnblack_3on,  pawnblack_4on,  pawnblack_5on,  pawnblack_6on,  pawnblack_7on,  pawnblack_8on;
	
		//Variables para torres blancas.
		wire [7:0] rook_white_rgb, rook_2white_rgb;
		wire [2:0]  rook_1on, rook_2on;
		
		//Variables para torres negras.
		wire [7:0] rook_black_rgb,rook_2black_rgb;
		wire [2:0]  rookblack_1on, rookblack_2on;
		
		//Variables para caballos blancos.
		wire [7:0] knight_white_rgb,knight_2white_rgb;
		wire [2:0]  knight_1on, knight_2on;
		
		//Variables para caballos negros.
		wire [7:0] knight_black_rgb,knight_2black_rgb;
		wire [2:0]  knightblack_1on, knightblack_2on;
		
		//Variables para alfil blanco.
		wire [7:0] bishop_white_rgb,bishop_2white_rgb;
		wire [2:0]  bishop_1on, bishop_2on;
		
		//Variables para alfil negro.
		wire [7:0] bishop_black_rgb,bishop_2black_rgb;
		wire [2:0]  bishopblack_1on, bishopblack_2on;
		
		//Variables para rey blanco.
		wire [7:0] king_white_rgb;
		wire [2:0]  king_on;
		
		//Variables para rey negro.
		wire [7:0] king_black_rgb;
		wire [2:0]  kingblack_on;
		
		//Variables para reina blanco.
		wire [7:0] queen_white_rgb;
		wire [2:0]  queen_on;
		
		//Variables para reina negro.
		wire [7:0] queen_black_rgb;
	   wire [2:0]  queenblack_on;
		
	 localparam SQUARE_SIZE = 60;
		
	 //Instancia del modulo de sincronizacion de VGA
	 vga_sync vsync_unit (clk_i,rst_i,hsync_o,vsync_o,video_on, pixel_x, pixel_y);
	
	//Instancia del modulo de dibujar el tablero
	 table_unit tablero(pixel_x, pixel_y, square_rgb, square_white_on, square_black_on);
	 
	 //Piezas Blancas:
	 
	 //PEONES
	 pawn_unit pawn_1w(pixel_x, pixel_y,0*SQUARE_SIZE,6*SQUARE_SIZE,  color, pawn_white_rgb, pawn_1on);	
	 pawn_unit pawn_2w(pixel_x, pixel_y,1*SQUARE_SIZE,6*SQUARE_SIZE,  color, pawn_2white_rgb, pawn_2on);	 
	 pawn_unit pawn_3w(pixel_x, pixel_y,2*SQUARE_SIZE,6*SQUARE_SIZE,  color, pawn_3white_rgb, pawn_3on);	 
	 pawn_unit pawn_4w(pixel_x, pixel_y,3*SQUARE_SIZE,6*SQUARE_SIZE,  color, pawn_4white_rgb, pawn_4on);	 
	 pawn_unit pawn_5w(pixel_x, pixel_y,4*SQUARE_SIZE,6*SQUARE_SIZE,  color, pawn_5white_rgb, pawn_5on);	 
	 pawn_unit pawn_6w(pixel_x, pixel_y,5*SQUARE_SIZE,6*SQUARE_SIZE,  color, pawn_6white_rgb, pawn_6on);	 
	 pawn_unit pawn_7w(pixel_x, pixel_y,6*SQUARE_SIZE,6*SQUARE_SIZE,  color, pawn_7white_rgb, pawn_7on);	
	 pawn_unit pawn_8w(pixel_x, pixel_y,7*SQUARE_SIZE,6*SQUARE_SIZE,  color, pawn_8white_rgb, pawn_8on);

	 //TORRES
	 rook_unit rock_1w(pixel_x, pixel_y,0*SQUARE_SIZE,7*SQUARE_SIZE, color, rook_white_rgb, rook_1on);
	 rook_unit rock_2w(pixel_x, pixel_y,7*SQUARE_SIZE,7*SQUARE_SIZE, color, rook_2white_rgb, rook_2on);
	 
	 //CABALLOS	
	 knight_unit knight_1w(pixel_x, pixel_y,1*SQUARE_SIZE,7*SQUARE_SIZE, color, knight_white_rgb, knight_1on);
	 knight_unit knight_2w(pixel_x, pixel_y,6*SQUARE_SIZE,7*SQUARE_SIZE, color, knight_2white_rgb, knight_2on);
	 
	 //ALFIL
	 bishop_unit bishop_1w(pixel_x, pixel_y,2*SQUARE_SIZE,7*SQUARE_SIZE, color, bishop_white_rgb, bishop_1on);
	 bishop_unit bishop_2w(pixel_x, pixel_y,5*SQUARE_SIZE,7*SQUARE_SIZE, color, bishop_2white_rgb, bishop_2on);
	 
	 //REY
	 king_unit king_w(pixel_x, pixel_y,4*SQUARE_SIZE,7*SQUARE_SIZE, color, king_white_rgb, king_on);
	 
	 //REINA
	 queen_unit queen_w(pixel_x, pixel_y,3*SQUARE_SIZE,7*SQUARE_SIZE, color, queen_white_rgb, queen_on);
	 
	 //Piezas Negras:
	 
	 //PEONES
	 pawn_unit pawn_1b(pixel_x, pixel_y,0*SQUARE_SIZE,1*SQUARE_SIZE,  ~color, pawn_black_rgb, pawnblack_1on);	
	 pawn_unit pawn_2b(pixel_x, pixel_y,1*SQUARE_SIZE,1*SQUARE_SIZE,  ~color, pawn_2black_rgb, pawnblack_2on);	 
	 pawn_unit pawn_3b(pixel_x, pixel_y,2*SQUARE_SIZE,1*SQUARE_SIZE,  ~color, pawn_3black_rgb, pawnblack_3on);	 
	 pawn_unit pawn_4b(pixel_x, pixel_y,3*SQUARE_SIZE,1*SQUARE_SIZE,  ~color, pawn_4black_rgb, pawnblack_4on);	 
	 pawn_unit pawn_5b(pixel_x, pixel_y,4*SQUARE_SIZE,1*SQUARE_SIZE,  ~color, pawn_5black_rgb, pawnblack_5on);	 
	 pawn_unit pawn_6b(pixel_x, pixel_y,5*SQUARE_SIZE,1*SQUARE_SIZE,  ~color, pawn_6black_rgb, pawnblack_6on);	 
	 pawn_unit pawn_7b(pixel_x, pixel_y,6*SQUARE_SIZE,1*SQUARE_SIZE,  ~color, pawn_7black_rgb, pawnblack_7on);	
	 pawn_unit pawn_8b(pixel_x, pixel_y,7*SQUARE_SIZE,1*SQUARE_SIZE,  ~color, pawn_8black_rgb, pawnblack_8on);

	 //TORRES
	 rook_unit rock_1b(pixel_x, pixel_y,0*SQUARE_SIZE,0*SQUARE_SIZE, ~color, rook_black_rgb, rookblack_1on);
	 rook_unit rock_2b(pixel_x, pixel_y,7*SQUARE_SIZE,0*SQUARE_SIZE, ~color, rook_2black_rgb, rookblack_2on);
	 
	 //CABALLOS	
	 knight_unit knight_1b(pixel_x, pixel_y,1*SQUARE_SIZE,0*SQUARE_SIZE, ~color, knight_black_rgb, knightblack_1on);
	 knight_unit knight_2b(pixel_x, pixel_y,6*SQUARE_SIZE,0*SQUARE_SIZE, ~color, knight_2black_rgb, knightblack_2on);
	 
	 //ALFIL
	 bishop_unit bishop_1b(pixel_x, pixel_y,2*SQUARE_SIZE,0*SQUARE_SIZE, ~color, bishop_black_rgb, bishopblack_1on);
	 bishop_unit bishop_2b(pixel_x, pixel_y,5*SQUARE_SIZE,0*SQUARE_SIZE, ~color, bishop_2black_rgb, bishopblack_2on);
	 
	 //REY
	 king_unit king_b(pixel_x, pixel_y,4*SQUARE_SIZE,0*SQUARE_SIZE, ~color, king_black_rgb, kingblack_on);
	 
	 //REINA
	 queen_unit queen_b(pixel_x, pixel_y,3*SQUARE_SIZE,0*SQUARE_SIZE, ~color, queen_black_rgb, queenblack_on);
	 
	 
	 // MUX de seleccion de salida para senales de color RGB
	always @*
		if (video_on && ~rst_i)
			//if(square_white_on || square_black_on || pawn_on)
			//	rgb_o <= (square_white_on || square_black_on) ? square_rgb: pawn_white_rgb;
			if(pawn_1on)
				rgb_o <= pawn_white_rgb;
			else if(pawn_2on)
				rgb_o <= pawn_2white_rgb;
			else if(pawn_3on)
					rgb_o <= pawn_3white_rgb;
			else if(pawn_4on)
							rgb_o <= pawn_4white_rgb;
			else if(pawn_5on)
							rgb_o <= pawn_5white_rgb;
			else if(pawn_6on)
							rgb_o <= pawn_6white_rgb;
			else if(pawn_7on)
							rgb_o <= pawn_7white_rgb;
			else if(pawn_8on)
							rgb_o <= pawn_8white_rgb;	
			else if (rook_1on)
				rgb_o <= rook_white_rgb;
			else if (rook_2on)
				rgb_o <= rook_2white_rgb;
		   else if (knight_1on)
					rgb_o <= knight_white_rgb;
			else if (knight_2on)
					rgb_o <= knight_2white_rgb;
		   else if (bishop_1on)
					rgb_o <= bishop_white_rgb;
			else if (bishop_2on)
					rgb_o <= bishop_2white_rgb;
			else if (king_on)
					rgb_o <= king_white_rgb;
			else if (queen_on)
					rgb_o <= queen_white_rgb;
		   else if(pawnblack_1on)
				rgb_o <= pawn_black_rgb;
			else if(pawnblack_2on)
				rgb_o <= pawn_2black_rgb;
			else if(pawnblack_3on)
					rgb_o <= pawn_3black_rgb;
			else if(pawnblack_4on)
							rgb_o <= pawn_4black_rgb;
			else if(pawnblack_5on)
							rgb_o <= pawn_5black_rgb;
			else if(pawnblack_6on)
							rgb_o <= pawn_6black_rgb;
			else if(pawnblack_7on)
							rgb_o <= pawn_7black_rgb;
			else if(pawnblack_8on)
							rgb_o <= pawn_8black_rgb;	
			else if (rookblack_1on)
				rgb_o <= rook_black_rgb;
			else if (rookblack_2on)
				rgb_o <= rook_2black_rgb;
		   else if (knightblack_1on)
					rgb_o <= knight_black_rgb;
			else if (knightblack_2on)
					rgb_o <= knight_2black_rgb;
		   else if (bishopblack_1on)
					rgb_o <= bishop_black_rgb;
			else if (bishopblack_2on)
					rgb_o <= bishop_2black_rgb;
			else if (kingblack_on)
					rgb_o <= king_black_rgb;
			else if (queenblack_on)
					rgb_o <= queen_black_rgb;
			else if (square_white_on || square_black_on)
				rgb_o <= (square_white_on || square_black_on) ? square_rgb: pawn_white_rgb;
			else 
				rgb_o <= 0;
		else
			rgb_o <= 0;

endmodule
